﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.IO;

public partial class dang_bai : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["UserEmail"] == null)
            Response.Redirect("NotLogin.aspx");
        if (!IsPostBack)
        {
            CKEditorControl1.Language = "vi";
            CKFinder.FileBrowser _FileBrowser = new CKFinder.FileBrowser();
            _FileBrowser.BasePath = "/ckfinder/";
            _FileBrowser.SetupCKEditor(CKEditorControl1);
            GetToPic();
            GetTopNews();
            GetTopPoster();

        }
    }
    protected void GetToPic()
    {
        SQLHelper Dal = new SQLHelper();
        Dal.CommandType = CommandType.StoredProcedure;
        Dal.CommandText = "SP_GetAllPostscategories";
        DataTable db = Dal.GetDataTable();
        ddlPostTopic.Items.Add("--------------");
        ddlPostTopic.AppendDataBoundItems = true;
        ddlPostTopic.DataValueField = db.Columns[0].ToString();
        ddlPostTopic.DataTextField = db.Columns[1].ToString();
        ddlPostTopic.DataSource = db;
        ddlPostTopic.DataBind();
        ListItemCollection lic = new ListItemCollection();
        lic = ddlPostTopic.Items;
        foreach (ListItem li in lic)
        {
            li.Attributes.Add("Style", "line-height:40px");
            li.Attributes.Add("Style", "border-bottom:solid thin #959595");
        }
        
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        PostNews();
        reset();
    }
    protected void PostNews()
    {
        if (FileUpload1.HasFile == true)
        {
            
                string filename = Path.GetFileName(FileUpload1.FileName);
                FileUpload1.SaveAs(Server.MapPath("Images/thumb-news/" + filename));
                SQLHelper Dal = new SQLHelper();
                Dal.Open();
                Dal.CommandType = CommandType.StoredProcedure;
                Dal.CommandText = "SP_AddPost";
                Dal.ParameterCollection = new string[6] { "@TieuDe", "@MoTa", "@NoiDung", "@HinhDaiDien","@UserEmail", "@ChuDeID" };
                Dal.ValueCollection = new string[6] { txtPostTitle.Text, txtShorcut.Text,CKEditorControl1.Text,"Images/thumb-news/" +filename,Session["UserEmail"].ToString(), ddlPostTopic.SelectedValue};
                Dal.ExecuteNonQuery();
                Dal.Close();
                Dal.Dispose();
        }
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        reset();
    }
    protected void reset()
    {
        txtShorcut.Text = "";
        txtPostTitle.Text = "";
        ddlPostTopic.SelectedIndex = -1;
        CKEditorControl1.Text = "";
    }
    protected void GetTopNews()
    {
        SQLHelper Dal = new SQLHelper();
        Dal.CommandType = CommandType.StoredProcedure;
        Dal.CommandText = "SP_GetTopHotNews";
        DataTable db = Dal.GetDataTable();
        rptTopHotNews.DataSource = db;
        rptTopHotNews.DataBind();
    }
    protected void GetTopPoster()
    {
        SQLHelper Dal = new SQLHelper();
        Dal.CommandType = CommandType.StoredProcedure;
        Dal.CommandText = "SP_GetTopPoster";
        DataTable db = Dal.GetDataTable();
        rptTopPoster.DataSource = db;
        rptTopPoster.DataBind();
    }
}
